using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Scheduler;
using System.Collections.Generic;

public partial class Website_Booking : System.Web.UI.Page
{
    protected static LinkedList<Therapy> therapyTypes = null;
    protected static LinkedList<String> rmtList_Name = null;
    protected static LinkedList<String> rmtList_ID = null;
    protected static String rmtID = "";
    protected static String rmtName = "";
    protected static RMT rmt = null;
    protected static Customer cust = null;
    protected static String therapy = "";
    protected static String room = "";
    protected static int price = 0;
    protected static int duration = 0;
    protected static String userID;
    protected static int userType = 2;

    /*
     * This method initializes the components on the page
     */
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
            return;

        
        Database DB = new Database();
        DB.OpenDB();

        if (Context.Items["User_ID"] != null)
        {
            userID = Context.Items["User_ID"].ToString();
            cust = new Customer(DB, userID);
        }

        therapyTypes = Therapy.readAllTherapies(DB);

        rmtList_Name =  RMT.readAllRMTs_Name(DB);
        rmtList_ID = RMT.readAllRMTs_ID(DB);      
        foreach (String name in rmtList_Name)
            dlstRMT.Items.Add(name);

        // Get information about the RMT chosen in the drop down list.
        rmtID = rmtList_ID.First.Value; //  Get the ID of the RMT
        rmtName = rmtList_Name.First.Value; // Get the Name of the RMT
        rmt = new RMT(DB, rmtID); // Create an RMT object and read all its information from the database
        displayTherapies();

        // Show the user that he can book an appointment from 8am till 19:30am
        // Display the hours (from 8 to 19):
        for (int i = 8; i < 20; i++)
            dlstStartTime_hours.Items.Add(i.ToString());

        // Display the inbetween hour intervals (0 minutes and 30 minutes):
        dlstStartTime_minutes.Items.Add("00");
        dlstStartTime_minutes.Items.Add("30");

        DB.CloseDB();
    }

    protected void dlstRMT_SelectedIndexChanged(object sender, EventArgs e)
    {
        Database DB = new Database();
        DB.OpenDB();

        // Get information about the RMT chosen in the drop down list.
        rmtName = dlstRMT.SelectedValue.ToString(); // Get the Name of the RMT
        rmt = new RMT(DB, rmtName); // Create an RMT object and read all its information from the database
        rmtID = rmt.ID; // Get the ID of the RMT
        displayTherapies();
        
        DB.CloseDB();

    }

    /*
     * This method displays the therapies for the selected RMT
     */
    protected void displayTherapies()
    {
        // Transverse the list of RMTs to find the RMT we are looking for.
        // The RMT we are looking for has the same name as the one selected in
        // the RMT drop down list.
        // Then display the therapies that this RMT provides in the
        // Therapy drop down list.

        // Now use this information to transverse the list of all available therapies.
        // See which therapies this RMT provides, and then only display those
        // therapies.

        // Display only the therapies that this RMT provides
        foreach (Therapy therapy_type in therapyTypes)
        {
            if (rmt.TherapyList.Contains(therapy_type))
                dlstTherapy.Items.Add(therapy_type.Name);
        }
    }

    /*
     * This method processes the book appointment request based 
     * on the given information
     */
    protected void cmdAppointment_Click(object sender, EventArgs e)
    {
        int startMin = int.Parse(dlstStartTime_minutes.SelectedValue),
            startHour = int.Parse(dlstStartTime_hours.SelectedValue);

        Database DB = new Database();
        DB.OpenDB();

        room = dlstRoomName.SelectedValue;

        Room curRoom = new Room(DB, room);
        DateTime selectedDate = clndrDate.SelectedDate;
        DateTime startTime = new DateTime( selectedDate.Year, selectedDate.Month, selectedDate.Day,
                                           startHour, startMin, 0);
        //Build timeslot
        DateTime endTime = startTime.AddMinutes(duration);
        TimeSlot desiredTimeSlot = new TimeSlot(startTime.Year, startTime.Month,
                                startTime.Day, startTime.Hour, startTime.Minute,
                                endTime.Hour, endTime.Minute);


        if (curRoom.bookAppointment(DB, rmt.Name, cust.Name,
                      therapy, desiredTimeSlot))
        {

            //try to book appointment...
            cust.updateAppointments(DB, rmt.Name, therapy,
                                         room, desiredTimeSlot, price);
            /*
             * redirect the customer to the profile page
             */
            Context.Items["User_ID"] = userID;
            Context.Items["User_Type"] = userType.ToString();
            Server.Transfer("UserProfile.aspx");
        }
        else
        {
            lblFail.Visible = true;
        }
        

        DB.CloseDB();
    }

    /*
     * This method handles the therapy drop down list index changed event
     */
    protected void dlstTherapy_SelectedIndexChanged(object sender, EventArgs e)
    {
        Database DB = new Database();
        DB.OpenDB();

        therapy = dlstTherapy.SelectedValue; // get the therapy type
        Therapy theraypData = new Therapy(DB, rmtID, therapy); // create a therapy object with the price list
        LinkedList<TherapyPrice> pricesList = theraypData.PriceList; // get the price list

        // Clear the previous options in the price list because the previous list
        // pertained to the previous selection of RMT and therapy type.
        rbtnAppDuration.Items.Clear();

        // populate the radio checklist with all the prices and durations
        foreach (TherapyPrice price in pricesList)
            rbtnAppDuration.Items.Add(price.Duration + " minutes costs " + price.Price);

        DB.CloseDB();

    }
    /*
     * This method handles the duration radio button selection event
     */
    protected void rbtnAppDuration_SelectedIndexChanged(object sender, EventArgs e)
    {
        String strDurationPrice = rbtnAppDuration.SelectedValue;
        String strDuration = "";
        String strPrice = "";

        // Copy the first number (which is the duration) in the strDurationPrice string
        // convert the string to a character array
        // and then find the first occurance of a space
        // take a substring of this strDurationPrice string
        char[] charDurationPrice = strDurationPrice.ToCharArray();
        int length = charDurationPrice.Length;
        int copyLength = 0; // the amount of characters to copy till the first whitespace
        for (copyLength = 0; copyLength < length; copyLength++)
        {
            if (charDurationPrice[copyLength] == ' ')
                break;
        }
        strDuration = strDurationPrice.Substring(0, copyLength);

        // Copy the first number (which is the price) in the strDurationPrice string
        // convert the string to a character array
        // and then find the first occurance of a space
        // take a substring of this strDurationPrice string
        for (copyLength = length-1; copyLength >=0 ; copyLength--)
        {
            if (charDurationPrice[copyLength] == ' ')
                break;
        }
        strPrice = strDurationPrice.Substring(copyLength+1, length-copyLength-1);
        price = Convert.ToInt32(strPrice);

        if (strDuration != "" && strDuration != null)
            duration = Convert.ToInt32(strDuration);
    }

    /*
     * This method handles the click even for the profile click
     * it redirects the user to the profile page
     */
    protected void btnProfile_Click(object sender, EventArgs e)
    {
        Context.Items["User_ID"] = userID;
        Context.Items["User_Type"] = userType.ToString();
        Server.Transfer("UserProfile.aspx");
    }
}
